//
//  AppTools.m
//  豆瓣
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
@implementation AppTools
+ (void)getAFN:(NSString *)url block:(AppToolsAFN)block
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    //NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        id result = responseObject;
        block (result);
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@",path_doc);
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        NSLog(@"%@",path_doc);

        NSLog(@"失败==== %@",error);
        
    }];

}
+ (NSString *)flattenHTML:(NSString *)html {
    
    NSScanner *theScanner;
    NSString *text = nil;
    
    theScanner = [NSScanner scannerWithString:html];
    
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:
                [NSString stringWithFormat:@"%@>", text]
                                               withString:@""];
    } // while //
    
    return html;
}

#pragma mark -- 计算内容的高度第一步:封装算法

+ (CGFloat)heightForContent:(NSString *)content font:(CGFloat)font width:(CGFloat)width{
    //参数1.设置计算高度按照什么样的size来计算
    //注意:宽度一定与label一致,高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意:字体大小也要跟label字体一样
    //参数4.系统备用参数,设置为nil;
    CGSize size = CGSizeMake(width,10000 );
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil];
    CGRect frame =  [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}

#pragma mark -- 时间戳转换标准时间
+ (NSString *)timestampExchange:(NSInteger)time
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}


#pragma mark -- 距离现在时间
+ (NSString *)prettyDateWithReference:(NSDate *)reference {
    NSString *suffix = @" ";

    NSDate *date = [NSDate date];
    float different = [reference timeIntervalSinceDate:date];
    if (different < 0) {
        different = -different;
        suffix = @" ";
    }
    
    // days = different / (24 * 60 * 60), take the floor value
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
//    int weeks  = (int)ceil(dayDifferent / 7);
//    int months = (int)ceil(dayDifferent / 30);
//    int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1 分钟前 %@", suffix];
        }
        
        // lower than 60 minutes
        if (different < 660 * 60) {
            return [NSString stringWithFormat:@"%d 分钟 %@", (int)floor(different / 60), suffix];
        }
        
        // lower than 60 * 2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1 小时前 %@", suffix];
        }
        
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d 小时 %@", (int)floor(different / 3600), suffix];
        }
    }
    // lower than one week
    else if (days < 7) {
        
        return [NSString stringWithFormat:@"%d 天%@ %@", days, days == 1 ? @"前" : @"", suffix];
    }else{
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        return [NSString stringWithFormat:@"%@",[formatter stringFromDate:reference]];
    }
//    // lager than one week but lower than a month
//    else if (weeks < 4) {
//        return [NSString stringWithFormat:@"%d 周%@ %@", weeks, weeks == 1 ? @"前" : @"", suffix];
//    }
//    // lager than a month and lower than a year
//    else if (months < 12) {
//        return [NSString stringWithFormat:@"%d 月%@ %@", months, months == 1 ? @"前" : @"", suffix];
//    }
//    // lager than a year
//    else {  
//        return [NSString stringWithFormat:@"%d 年%@ %@", years, years == 1 ? @"前" : @"", suffix];
//    }  
    
    return self.description;
}
@end
